<script setup>
import { ref } from 'vue'
import { Lock } from '@element-plus/icons-vue'

const PwdData = ref({
    old_pwd: '',
    new_pwd: '',
    re_pwd: ''
})
//校验密码的函数
const checkRePassword = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次确认密码'))
    } else if (value !== PwdData.value.new_pwd) {
        callback(new Error('请确保两次输入的密码一样'))
    } else {
        callback()
    }
}
//定义表单校验规则
const rules = {
    old_pwd: [
        { required: true, message: '请输入旧密码', trigger: 'blur' },
        { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
    ],
    new_pwd: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        { min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur' }
    ],
    re_pwd: [
        { validator: checkRePassword, trigger: 'blur' }
    ]
}
import { ElMessage } from 'element-plus';
import { updateUserPwdService } from '@/api/user.js'
import { useRouter } from 'vue-router'
const router = useRouter();
import { useTokenStore } from '@/stores/token.js'
const tokenStore = useTokenStore();
import { useInfoStore } from '@/stores/userInfo.js'
const userInfo = useInfoStore();
const updateUserPwd = async () => {
    let result = await updateUserPwdService(PwdData.value);
    ElMessage.success(result.message ? result.message : "修改密码成功");
    //清除token，并跳转登录界面
    //1.清空pinia中存储的token以及个人信息
    tokenStore.removeToken();
    userInfo.removeUserInfo();
    router.push('/login');
}

//重置表单数据
const resetForm = () => {
    PwdData.value = {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
    }
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>重置密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <!-- 注册表单 -->
                <el-form ref="form" size="large" autocomplete="off" :model="PwdData" :rules="rules">
                    <el-form-item>
                        <h1>注册</h1>
                    </el-form-item>
                    <el-form-item prop="old_pwd">
                        <el-input :prefix-icon="Lock" placeholder="原密码" type="password"
                            v-model="PwdData.old_pwd"></el-input>
                    </el-form-item>
                    <el-form-item prop="new_pwd">
                        <el-input :prefix-icon="Lock" type="password" placeholder="新密码"
                            v-model="PwdData.new_pwd"></el-input>
                    </el-form-item>
                    <el-form-item prop="re_pwd">
                        <el-input :prefix-icon="Lock" type="password" placeholder="确认新密码"
                            v-model="PwdData.re_pwd"></el-input>
                    </el-form-item>
                    <!-- 注册按钮 -->
                    <el-form-item>
                        <el-button class="button" type="primary" auto-insert-space @click="updateUserPwd(); resetForm()">
                            修改密码
                        </el-button>
                        <el-button class="button" @click="resetForm()">重置</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>